var $MB = (function () {
    var bootstrapTable_default = {
        method: 'get',
        striped: true,
        cache: false,
        pagination: true,
        sortable: false,
        sidePagination: "server",
        pageNumber: 1,
        pageSize: 10,
        pageList: [5, 10, 25, 50, 100],
        strictSearch: true,
        showColumns: false,
        minimumCountColumns: 2,
        clickToSelect: true,
        uniqueId: "ID",
        cardView: false,
        detailView: false,
        smartDisplay: false,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1
            };
        }
    };

    function _initTable(id, settings) {
        var params = $.extend({}, bootstrapTable_default, settings);
        if (typeof params.url === 'undefined') {
            throw '初始化表格失败，请配置url参数！';
        }
        if (typeof params.columns === 'undefined') {
            throw '初始化表格失败，请配置columns参数！';
        }
        $('#' + id).bootstrapTable(params);
        $("body").on("click", "[data-table-action]", function (a) {
            a.preventDefault();
            var b = $(this).data("table-action");
            if ("excel" === b && $(this).closest(".dataTables_wrapper").find(".buttons-excel").trigger("click"), "csv" === b && $(this).closest(".dataTables_wrapper").find(".buttons-csv").trigger("click"), "print" === b && $(this).closest(".dataTables_wrapper").find(".buttons-print").trigger("click"), "fullscreen" === b) {
                var c = $(this).closest(".card");
                c.hasClass("card--fullscreen") ? (c.removeClass("card--fullscreen"), $("body").removeClass("data-table-toggled")) : (c.addClass("card--fullscreen"), $("body").addClass("data-table-toggled"))
            }
        })
    }

    // jquery treegird
    function _initTreeTable(id, setting) {
        $('#' + id).bootstrapTreeTable({
            id: setting.id, // 选取记录返回的值
            code: setting.code, // 用于设置父子关系
            parentCode: setting.parentCode, //jinhui.li 解决去除根节点数据后，子树无法正常显示问题
            rootCodeValue: setting.rootCodeValue, //jinhui.li 解决去除根节点数据后，子树无法正常显示问题
            parentColumn: !setting.parentColumn ? 'parentId' : setting.parentColumn, // 用于设置父子关系
            type: "GET", // 请求数据的ajax类型
            url: setting.url, // 请求数据的ajax的url
            ajaxParams: !setting.ajaxParams ? {} : setting.ajaxParams, // 请求数据的ajax的data属性
            expandColumn: !setting.expandColumn ? '1' : setting.expandColumn, // 在哪一列上面显示展开按钮
            striped: true, // 是否各行渐变色
            bordered: true,
            checkboxes: true,
            expandAll: setting.expandAll ? true : setting.expandAll, // 是否全部展开
            columns: setting.columns

        });
    }

    /*--------------------------------------
        Bootstrap Notify Notifications
    ---------------------------------------*/
    function _notify(message, type, callback) {
        $.notify({
            icon: "fa fa-check",
            title: "",
            message: message,
            url: ''
        }, {
            element: 'body',
            type: type,
            allow_dismiss: true,
            placement: {
                from: "top",
                align: "center"
            },
            offset: {
                x: 20,
                y: 20
            },
            spacing: 10,
            z_index: 3001,
            delay: 2500,
            timer: 1000,
            url_target: '_blank',
            mouse_over: false,
            animate: {
                enter: "animated fadeInDown",
                exit: "animated fadeOutUp"
            },
            template: '<div data-notify="container" class="alert alert-dismissible alert-{0} alert--notify" role="alert">' +
            '<span data-notify="icon"></span> ' +
            '<span data-notify="title">{1}</span> ' +
            '<span data-notify="message" style="font-weight: 600">{2}</span>' +
            '<div class="progress" data-notify="progressbar">' +
            '<div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0;"></div>' +
            '</div>' +
            '<a href="{3}" target="{4}" data-notify="url"></a>' +
            '<button type="button" aria-hidden="true" data-notify="dismiss" class="alert--notify__close"><span style="background-color: rgba(255,255,255,.2);line-height: 19px;height: 20px;width: 20px;border-radius: 50%;font-size: 1.1rem;display: block;" aria-hidden="true">×</span></button>' +
            '</div>',
            onClosed: callback
        });
    }

    // close modal
    function _closeModal(modalId) {
        $("#" + modalId).find("button.btn-hide").attr("data-dismiss", "modal").trigger('click');
    }

    // close and reset modal
    function _closeAndRestModal(modalId) {
        var $modal = $("#" + modalId);
        $modal.find("button.btn-hide").attr("data-dismiss", "modal").trigger('click');
        $modal.find("form")[0].reset();
    }

    // 获取主题对应的16进制颜色
    function _getThemeColor(theme) {
        var color;
        switch (theme) {
            case 'green':
                color = '#32c787';
                break;
            case 'blue':
                color = '#337ab7';
                break;
            case 'red':
                color = '#ff5652';
                break;
            case 'orange':
                color = '#FF9800';
                break;
            case 'teal':
                color = '#39bbb0';
                break;
            case 'cyan':
                color = '#00BCD4';
                break;
            case 'blue-grey':
                color = '#607D8B';
                break;
            case 'purple':
                color = '#d559ea';
                break;
            case 'indigo':
                color = '#3F51B5';
                break;
            case 'lime':
                color = '#CDDC39';
                break;
            default:
                color = '#337ab7';
        }
        return color;
    }

    // 获取主题对应的rgba(x,x,x,.1)颜色，用于日期选择插件
    function _getThemeRGBA(theme, opacity) {
        var color;
        switch (theme) {
            case 'green':
                color = 'rgba(50,199,135,' + opacity + ')';
                break;
            case 'blue':
                color = 'rgba(51,122,183,' + opacity + ')';
                break;
            case 'red':
                color = 'rgba(255,86,82,' + opacity + ')';
                break;
            case 'orange':
                color = 'rgba(255,152,0,' + opacity + ')';
                break;
            case 'teal':
                color = 'rgba(57,187,176,' + opacity + ')';
                break;
            case 'cyan':
                color = 'rgba(0,188,212,' + opacity + ')';
                break;
            case 'blue-grey':
                color = 'rgba(96,125,139,' + opacity + ')';
                break;
            case 'purple':
                color = 'rgba(213,89,234,' + opacity + ')';
                break;
            case 'indigo':
                color = 'rgba(63,81,181,' + opacity + ')';
                break;
            case 'lime':
                color = 'rgba(205,220,57,' + opacity + ')';
                break;
            default:
                color = 'rgba(51,122,183,' + opacity + ')';
        }
        return color;
    }

    // confirm弹窗
    function _confirm(settings, callback) {
        swal({
            text: settings.text,
            type: "warning",
            showCancelButton: (false === settings.showCancelButton) ? false : true,
            confirmButtonColor: "#337ab7",
            confirmButtonText: settings.confirmButtonText,
            cancelButtonText: settings.cancelButtonText || "取消",
            allowOutsideClick: false,
            allowEscapeKey: false,
            animation: false
        }).then(callback);
    }

    // 恢复jsTree，还原到初始化状态
    function _resetJsTree(id) {
        $('#' + id).jstree(true).close_all();
        $('#' + id).jstree(true).deselect_all();
    }

    // 重新加载数据，重绘jsTree
    function _refreshJsTree(id, fn) {
        $('#' + id).data('jstree', false).empty();
        fn;
    }

    /**
     * 日历
     * @param obj eles 日期输入框
     * @param boolean dobubble    是否为双日期（true）
     * @param boolean secondNot    有无时分秒（有则true）
     * @param function callback    回调函数
     * @return none
     */
    function _calenders(eles, dobubble, secondNot, callback) {
        var singleNot, formatDate;
        if (dobubble === true) {
            singleNot = false;
        } else {
            singleNot = true;
        }
        if (secondNot === true) {
            formatDate = "YYYY-MM-DD HH:mm:ss";
        } else {
            formatDate = "YYYY-MM-DD";
        }

        $(eles).daterangepicker({
            "singleDatePicker": singleNot,
            "timePicker": secondNot,
            "timePicker24Hour": secondNot,
            "timePickerSeconds": secondNot,
            "showDropdowns": true,
            "timePickerIncrement": 1,
            "linkedCalendars": false,
            "autoApply": true,
            "autoUpdateInput": false,
            "locale": {
                "direction": "ltr",
                "format": formatDate,
                "separator": "~",
                "applyLabel": "选取",
                "cancelLabel": "取消",
                "fromLabel": "From",
                "toLabel": "To",
                "customRangeLabel": "Custom",
                "daysOfWeek": [
                    "日",
                    "一",
                    "二",
                    "三",
                    "四",
                    "五",
                    "六"
                ],
                "monthNames": [
                    "一月",
                    "二月",
                    "三月",
                    "四月",
                    "五月",
                    "六月",
                    "七月",
                    "八月",
                    "九月",
                    "十月",
                    "十一月",
                    "十二月"
                ],
                "firstDay": 1
            }
        }, function (start, end, label) {
            if (secondNot === true && dobubble === true) {
                $(eles).val($.trim(start.format('YYYY-MM-DD HH:mm:ss') + '~' + end.format('YYYY-MM-DD HH:mm:ss')));
            } else if (secondNot === false && dobubble === true) {
                $(eles).val($.trim(start.format('YYYY-MM-DD') + '~' + end.format('YYYY-MM-DD')));
            } else if (secondNot === false && dobubble === false) {
                $(eles).val(start.format('YYYY-MM-DD'));
            } else if (secondNot === true && dobubble === false) {
                $(eles).val(start.format('YYYY-MM-DD HH:mm:ss'));
            }
            if (typeof callback === 'function') {
                callback();
            }
        });
    }

    /**
     * i18n初始化
     * @param {string} type 语言类型 
     * @param {function} callback 回调函数
     */
    $.i18n.properties({
        name: 'mess', // 资源文件名称
        path: '/i18n/', // 资源文件所在目录路径
        mode: 'map', // 模式：变量或 Map 
        language: LANGUAGE_CODE == 'en_US' ? 'en_US' : 'zh_CN', // 对应的语言
        cache: false,
        debug: false,
        encoding: 'UTF-8'
    });

    /**
     * 国际化调用函数
     * @param {string} key 字段key
     */
    function _i18n(key) {
        var _i18nStr = $.i18n.prop(key);
        return _i18nStr;
    }

        function _tpvDatePicker(id) {
            var timePicker = $(id).datetimepicker({
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            });
        }

    function _tpvTimePicker(id) {
        var timePicker = $(id).datetimepicker({
            weekStart: 1,
            autoclose: 1,
            startView: 1,
            minView: 0,
            maxView: 1,
            forceParse: 0,
            minuteStep:15
        });
    }

    function _initTpvMeetingElement(){
       $('.tpv-icheck').iCheck({
           checkboxClass: 'icheckbox_minimal-tpv',
           radioClass: 'iradio_minimal-tpv',
           increaseArea: '20%'
       });

       _tpvDatePicker(".tpv-date-picker");
       _tpvTimePicker(".tpv-time-picker");
    }

    //Added by kevin@20181206 add date form function
    Date.prototype.format = function (fmt) {
    var o = {
    "m+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "i+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
    }

    //add by aaron.wei 2018.12.26
    if (!String.prototype.trim) {
        /*---------------------------------------
        * 清除字符串两端空格，包含换行符、制表符
        *---------------------------------------*/
        String.prototype.trim = function () { 
            return this.triml().trimr(); 
        }
    
        /*----------------------------------------
        * 清除字符串左侧空格，包含换行符、制表符
        * ---------------------------------------*/
        String.prototype.triml = function () {
            return this.replace(/^[\s\n\t]+/g, "");
        }
    
        /*----------------------------------------
        * 清除字符串右侧空格，包含换行符、制表符
        *----------------------------------------*/
        String.prototype.trimr = function () {
            return this.replace(/[\s\n\t]+$/g, "");
        }
    }

    //Added by kevin@20190110 format datetime
    function _formatDate(start, end) {
        var startDate = new Date(start);
        var endDate = new Date(end);

        if(startDate.getDate() == endDate.getDate()
                    && startDate.getMonth() == endDate.getMonth()
                    && startDate.getFullYear() == endDate.getFullYear()) {
            return startDate.format("yyyy-mm-dd hh:ii") + "-" + endDate.format("hh:ii");
        }

        return startDate.format("yyyy-mm-dd hh:ii") + "-" + endDate.format("yyyy-mm-dd hh:ii");
    }

    /**
     * add by aaron.wei 2018.12.17
     */
    $.ajaxSetup({ 
        asycn: false,
        xhrFields: {
            withCredentials: true
        },
        error: function (XMLHttpRequest, textStatus, errorThrown){
            if(XMLHttpRequest.status==403){
                _notify('您没有权限访问此资源或进行此操作', "danger");
                return false;
            }
            if(XMLHttpRequest.status==404){
                _notify('页面建设中...', "danger");
                return false;
            }
        }, 
        contentType:"application/x-www-form-urlencoded;charset=utf-8",
        complete: function (XMLHttpRequest,textStatus) {
            //通过XMLHttpRequest取得响应头,sessionstatus
            var sessionstatus = XMLHttpRequest.getResponseHeader("sessionStatus"); 
            //通过XMLHttpRequest取得返回的json
            var json = XMLHttpRequest.responseJSON;
            //通过XMLHttpRequest取得返回的text
            var text = XMLHttpRequest.responseText;
            if (text.indexOf('tpv-login-layout') !== -1 && text.indexOf('tpv-login-from') !== -1) {
                if(window.sessionStorage) {
                    //记录当前激活url
                    var url = $('#navigation span.navigation__active[onclick="loadMain(this);"]').attr('name');
                    sessionStorage.setItem(userName, url)
                }
                top.location.href = ctx +"login";
                return;
            }
        }
    });

    // 验证手机或固定电话 add by aaron.wei 2019.01.11
    if ($.validator) {
        $.validator.addMethod("isCheckTel", function(value, element) {
            //var reg = /^((0?1[345789]\d{9})|((0(10|2[1-3]|[3-9]\d{2})\-)?[1-9]\d{6,7}))$/;
            var reg = /([*\-\+#\d]){3,20}$/;
            return this.optional(element) || (reg.test(value));
        }, "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i>" + _i18n('vali.mobile.info'));
    }

    //获取指定key的hash值 add by aaron.wei 2019.01.11
    function _getHash(key, url) {
        var hash;
        if (!!url) {
            hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1");
            hash = (hash == url) ? "" : hash;
        } else {
            hash = self.location.hash;
        }
        hash = "" + hash;
        hash = hash.replace(/^[?#]/, '');
        if (hash) {
            return decodeURIComponent(hash);
        } else {
            return null;
        }
    }

    /**
     * 获取cookie
     * add by aaron.wei 2019.01.18
     */
    function _getCookie(name)
    {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return false;
    }

    return {
        initTable: function (id, setting) {
            _initTable(id, setting);
        },
        initTreeTable: function (id, setting) {
            _initTreeTable(id, setting);
        },
        getTableIndex: function (id, index) {
            var pageSize = $('#' + id).bootstrapTable('getOptions').pageSize;
            var pageNumber = $('#' + id).bootstrapTable('getOptions').pageNumber;
            return pageSize * (pageNumber - 1) + index + 1;
        },
        refreshTable: function (id) {
            $('#' + id).bootstrapTable('refresh');
        },
        n_default: function (message, callback) {
            _notify(message, "inverse", callback);
        },
        n_info: function (message, callback) {
            _notify(message, "info", callback);
        },
        n_success: function (message, callback) {
            _notify(message, "success", callback);
        },
        n_warning: function (message, callback) {
            _notify(message, "warning", callback);
        },
        n_danger: function (message, callback) {
            _notify(message, "danger", callback);
        },
        closeModal: function (modalId) {
            _closeModal(modalId);
        },
        closeAndRestModal: function (modalId) {
            _closeAndRestModal(modalId);
        },
        getThemeColor: function (theme) {
            return _getThemeColor(theme);
        },
        getThemeRGBA: function (theme, opacity) {
            return _getThemeRGBA(theme, opacity);
        },
        confirm: function (settings, callback) {
            _confirm(settings, callback);
        },
        resetJsTree: function (id) {
            _resetJsTree(id);
        },
        refreshJsTree: function (id, fn) {
            _refreshJsTree(id, fn);
        },
        calenders: function (eles, dobubble, secondNot, callback) {
            _calenders(eles, dobubble, secondNot, callback);
        },
        i18n: function (key) {
            return _i18n(key);
        },
        tpvTimePicker: function (id) {
            _tpvTimePicker(id);
        },
        tpvDatePicker: function (id) {
            _tpvDatePicker(id);
        },
        initTpvMeetingElement: function(){
           _initTpvMeetingElement();
        },
        formatDate: function(start, end) {
            return _formatDate(start, end);
        },
        getHash: function(key, url) {
            return _getHash(key, url); //add by aaron.wei 2019.01.11
        },
        getCookie: function(key) {
            return _getCookie(key); //add by aaron.wei 2019.01.18
        }
    }
})($);